package com.lzx.adx.report.support.strategy.channel;

import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import com.lzs.adx.commmon.utils.JsonUtils;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
@ProductType(ProductChannelConstant.YOU_DAO)
public class YouDaoReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("ip", reportDTO.getIp());
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        params.put("os", "android");
        params.put("ts", String.valueOf(System.currentTimeMillis()));
        params.put("model", "");
        params.put("developer_id", "9bc124de24e2416c");

        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) &&!StringUtils.contains(reportDTO.getImeiSum(),"__")){
            params.put("imei", StringUtils.upperCase(reportDTO.getImeiSum()));
        }

        if(StringUtils.isNotBlank(reportDTO.getOaid()) &&!StringUtils.contains(reportDTO.getOaid(),"__")){
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) &&!StringUtils.contains(reportDTO.getOaidSum(),"__")){
            params.put("oaid_md5", reportDTO.getOaidSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfa()) &&!StringUtils.contains(reportDTO.getIdfa(),"__")){
            params.put("idfa", reportDTO.getIdfa());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfaSum()) &&!StringUtils.contains(reportDTO.getIdfaSum(),"__")){
            params.put("idfa_md5", StringUtils.upperCase(reportDTO.getIdfaSum()));
        }
        if(StringUtils.isNotBlank(reportDTO.getHash_android_id()) &&!StringUtils.contains(reportDTO.getHash_android_id(),"__")){
            params.put("android_id_md5", reportDTO.getHash_android_id());
        }

        params.put("caid_idfa", "");
        params.put("caid", "");
        params.put("aaid", "");
        params.put("android_id", "");
        params.put("android_id_md5", "");
        params.put("reqid", "");
        log.info("回传给有道的原始数据：{}", JsonUtils.toJsonString(params));
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }
}
